package main

import (
	"errors"
	"fmt"
	"go_example/day01/Queue"
	"go_example/day01/StackArray"
	"io/ioutil"
)

func GetAll(path string, files[] string)([]string,error){
	read,err:=ioutil.ReadDir(path)//读取文件夹
	if err!=nil{
		return files,errors.New("文件夹不可读取")
	}

	for _, fi:=range read{
		if fi.IsDir(){//判读是否是文件夹
			fulldir:=path+"\\"+fi.Name() //构造新的路径
			files = append(files, fulldir)//追加路径
			files, _ = GetAll(fulldir, files) //文件夹递归处理
		}else {
			fulldir:=path+"\\"+fi.Name() //构造新的路径
			files = append(files, fulldir)//追加路径
		}
	}

	return files,nil
}

func main1() {
	path:= "D:\\gowork"
	files:=[]string{}
	files,_ = GetAll(path, files)
	for i:=0; i<len(files); i++ {
		fmt.Println(files[i] )
	}
}

func main2() {
	path:= "D:\\gowork"
	files:=[]string{}
	mystack := StackArray.NewStack()
	mystack.Push(path)

	for !mystack.IsEmpty(){
		getpath := mystack.Pop().(string)
		files = append(files, getpath)
		//fmt.Println(getpath)
		readDir, _ := ioutil.ReadDir(getpath)
		for _,fi := range readDir  {
			if fi.IsDir(){
				fulldir:=getpath+"\\"+fi.Name() //构造新的路径
				files = append(files, fulldir)//追加路径
				mystack.Push(fulldir)
			}else{
				fulldir:=path+"\\"+fi.Name() //构造新的路径
				files = append(files, fulldir)//追加路径
			}
		}
	}

	for i:=0; i<len(files); i++ {
		fmt.Println(files[i] )
	}
}

func main() {
	path:= "D:\\gowork"
	files:=[]string{}
	myQueue := Queue.NewQueue()
	myQueue.EnQueue(path)

	for !myQueue.IsEmpty(){
		getpath := myQueue.DeQueue().(string)
		files = append(files, getpath)
		//fmt.Println(getpath)
		readDir, _ := ioutil.ReadDir(getpath)
		for _,fi := range readDir  {
			if fi.IsDir(){
				fulldir:=getpath+"\\"+fi.Name() //构造新的路径
				files = append(files, fulldir)//追加路径
				myQueue.EnQueue(fulldir)
			}else{
				fulldir:=path+"\\"+fi.Name() //构造新的路径
				files = append(files, fulldir)//追加路径
			}
		}
	}

	for i:=0; i<len(files); i++ {
		fmt.Println(files[i] )
	}

}
